package com.chiry.business.entity;


import com.ruoyi.common.core.constant.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Result<T> {

    private int code;
    private String msg;
    private T data;

    public Result(ResultBuilder<T> builder) {
        this.msg = builder.msg;
        this.code = builder.code;
        this.data = builder.data;
    }

    public static <T> Result<T> success() {
        return (Result<T>) new ResultBuilder<>(HttpStatus.SUCCESS, "operation success").build();
    }

    public static <T> Result<T> success(T data) {
        return new ResultBuilder<>(HttpStatus.SUCCESS, "operation success", data).build();
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return (Result<T>) new ResultBuilder<>(resultCode.getCode(), resultCode.getMsg()).build();
    }

    public static <T> Result<T> fail(int code, String msg) {
        return (Result<T>) new ResultBuilder<>(code, msg).build();
    }

    public static <T> Result<T> fail(String msg) {
        return fail(HttpStatus.ERROR, msg);
    }

    public static <T> Result<T> fail() {
        return fail("operation fail");
    }

    public static <T> ResultBuilder<T> builder(int code, String msg) {
        return new ResultBuilder<>(code, msg);
    }

    public static <T> ResultBuilder<T> builder(int code, String msg, T data) {
        return new ResultBuilder<>(code, msg, data);
    }


    public static class ResultBuilder<T> {
        private int code;
        private String msg;
        private T data;

        public ResultBuilder(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public ResultBuilder(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        public ResultBuilder<T> code(int code) {
            this.code = code;
            return this;
        }

        public ResultBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public ResultBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public Result<T> build() {
            return new Result<>(this);
        }
    }

}
